package com.coffee.order.service.sku.impl;

import com.alibaba.fastjson.JSONObject;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import com.coffee.order.api.sku.query.PayOrderQuery;
import com.coffee.order.api.sku.result.PayOrderResult;
import com.coffee.order.api.sku.service.PayOrderService;
import com.coffee.order.service.sku.bean.PayOrder;
import com.coffee.order.service.sku.bean.WeixinNotifyParameter;
import com.coffee.order.service.sku.dao.PayOrderDao;
import com.coffee.order.service.sku.dao.WeixinNotifyParameterDao;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class PayOrderServiceImpl implements PayOrderService {
    @Resource
    private PayOrderDao payOrderDao;
    @Resource
    private WeixinNotifyParameterDao weixinNotifyParameterDao;

    @Override
    public Page<PayOrderResult> getPager(PayOrderQuery param, PagerInfo pr) {
        Page<PayOrderResult> pager = new Page<PayOrderResult>(pr.getStart(), pr.getPageSize());
        Integer count = payOrderDao.count(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<PayOrder> list = payOrderDao.page(param, pr.getStart(), pr.getPageSize());
            pager.setData(DTOUtils.convertList(list, PayOrderResult.class));
        }
        return pager;
    }

    @Override
    public Integer count(PayOrderQuery param) {
        Integer count = payOrderDao.count(param);
        return count;
    }

    @Override
    public PayOrderResult getDetail(Integer id) {
        PayOrder item = payOrderDao.findById(id);
        if (item == null) {
            return null;
        }
        PayOrderResult result = new PayOrderResult();
        BeanUtilExt.copyProperties(result, item);

        return result;
    }

    @Override
    public List<PayOrderResult> getList(PayOrderQuery param, PagerInfo pr) {
        List<PayOrder> list = payOrderDao.page(param, pr.getStart(), pr.getPageSize());
        return DTOUtils.convertList(list, PayOrderResult.class);
    }

    @Override
    public List<PayOrderResult> getList(PayOrderQuery param) {
        List<PayOrder> list = payOrderDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, PayOrderResult.class);
    }

    @Override
    public List<PayOrderResult> getList() {
        PayOrderQuery param = new PayOrderQuery();
        List<PayOrder> list = payOrderDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, PayOrderResult.class);
    }


    @Override
    public void delete(Integer id) {
        payOrderDao.deleteById(id);
    }

    @Override
    public void addOrUpdate(PayOrderResult result) throws BusinessException {
        PayOrder item = new PayOrder();
        BeanUtilExt.copyProperties(item, result);
        if (item.getId() == null) {
            item.setCreateTime(new Date());
            payOrderDao.insert(item);
        } else {
            PayOrder tmp = payOrderDao.findById(item.getId());
            if (tmp == null) {
                throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
            }
            payOrderDao.updateById(item);
        }
    }

    @Override
    public Map<Integer, PayOrderResult> getItemMap(Set<Integer> idSet) {
        Map<Integer, PayOrderResult> map = new HashMap<Integer, PayOrderResult>();
        List<PayOrder> list = payOrderDao.findByIds(StringUtil.setToList(idSet));
        List<PayOrderResult> resultList = DTOUtils.convertList(list, PayOrderResult.class);
        for (PayOrderResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Map<Integer, PayOrderResult> getItemMap() {
        Map<Integer, PayOrderResult> map = new HashMap<Integer, PayOrderResult>();
        PayOrderQuery param = new PayOrderQuery();
        List<PayOrder> list = payOrderDao.page(param, 0, Integer.MAX_VALUE);
        List<PayOrderResult> resultList = DTOUtils.convertList(list, PayOrderResult.class);
        for (PayOrderResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public void insertWeixinNotifyParameter(Map<String, String> map) {
        WeixinNotifyParameter param = new WeixinNotifyParameter();
        param.setResult(JSONObject.toJSONString(map));
        param.setBillNo(map.get("transaction_id"));
        param.setCreateTime(new Date());
        weixinNotifyParameterDao.insert(param);
    }


}
